package com.td.module.pests.controller.app.library;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.pests.controller.admin.library.vo.*;
import com.td.module.pests.convert.library.LibraryConvert;
import com.td.module.pests.dal.dataobject.library.LibraryDO;
import com.td.module.pests.service.library.LibraryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "app - 病虫害知识库")
@RestController
@RequestMapping("/pests/library")
@Validated
public class LibraryAppController {

    @Resource
    private LibraryService libraryService;

    @PostMapping("/create")
    @ApiOperation("创建病虫害知识库")
    public CommonResult<Long> createLibrary(@Valid @RequestBody LibraryCreateReqVO createReqVO) {
        return success(libraryService.createLibrary(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新病虫害知识库")
    public CommonResult<Boolean> updateLibrary(@Valid @RequestBody LibraryUpdateReqVO updateReqVO) {
        libraryService.updateLibrary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除病虫害知识库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteLibrary(@RequestParam("id") Long id) {
        libraryService.deleteLibrary(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得病虫害知识库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<LibraryRespVO> getLibrary(@RequestParam("id") Long id) {
        LibraryDO library = libraryService.getLibrary(id);
        return success(LibraryConvert.INSTANCE.convert(library));
    }

    @GetMapping("/list")
    @ApiOperation("获得病虫害知识库列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<LibraryRespVO>> getLibraryList(@RequestParam("ids") Collection<Long> ids) {
        List<LibraryDO> list = libraryService.getLibraryList(ids);
        return success(LibraryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得病虫害知识库分页")
    public CommonResult<PageResult<LibraryRespVO>> getLibraryPage(@Valid LibraryPageReqVO pageVO) {
        PageResult<LibraryDO> pageResult = libraryService.getLibraryPage(pageVO);
        return success(LibraryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出病虫害知识库 Excel")
    @OperateLog(type = EXPORT)
    public void exportLibraryExcel(@Valid LibraryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LibraryDO> list = libraryService.getLibraryList(exportReqVO);
        // 导出 Excel
        List<LibraryExcelVO> datas = LibraryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "病虫害知识库.xls", "数据", LibraryExcelVO.class, datas);
    }

}
